package com.only.common.doucment;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author only
 * @date 2023/11/1
 * @description 原 DataGrid 不支持生成文档，现在 使用 ResultLine 代替
 */
@Data
public class PageResult<T> {
    /**
     * 数据行
     */
    private List<T> rows;

    /**
     * 数据总数
     */
    private long total;

    public PageResult(List<T> lines, long total) {
        this.rows = lines;
        this.total = total;
    }

    public PageResult(long total) {
        this.total = total;
        this.rows = Collections.emptyList();
    }

    @JsonIgnore
    public boolean isEmpty() {
        return rows.isEmpty();
    }

    public <U> PageResult<U> convert(Function<? super T, U> mapper) {
        List<U> data = rows.stream().map(mapper).collect(Collectors.toList());
        return new PageResult<>(data, total);
    }
}
